Source code for /engineering/webperf/slave-v4[j1.2]/HTTPRequest.javaOriginal file HTTPRequest.java
   1 import java.io.*;
   2 import java.util.*;
   3 
   4 public class HTTPRequest {
   5    private DataInputStream is;
   6    String HTTPMethod;
   7    String HTTPPath;
   8    String HTTPProtocol;
   9    String HTTPContent;
  10    
  11    Hashtable HTTPHeaders;
  12          
  13    public HTTPRequest(DataInputStream is) {
  14       this.is = is;      
  15    }
  16 
  17    public void read() throws Exception {
  18       parseHeaders();
  19       try {
  20          if (HTTPMethod.equalsIgnoreCase("POST")) {
  21             try {
  22                readContent();
  23             } catch (Exception e) {
  24                System.out.println("Exception on read.  message=" + e);
  25 		   throw(e);
  26             }
  27          }
  28       } catch (Exception e) {
  29 	   throw(e); 
  30       }
  31    }
  32 
  33    public String method() {
  34       return HTTPMethod;
  35    }
  36    
  37    public String path() {
  38       return HTTPPath;
  39    }
  40    
  41    public String protocol() {
  42       return HTTPProtocol;
  43    }   
  44 
  45    public String content() {
  46       return HTTPContent;
  47    }
  48       
  49    private void parseHeaders() throws Exception {   
  50       String input;
  51       String request;
  52       int pos;
  53       
  54       try {    
  55          HTTPHeaders = new Hashtable();
  56          while ((input = is.readLine()) != null &&  !input.equals("")) {
  57             // Parse first line separately
  58             if (HTTPMethod == null) {
  59                parseRequestLine(input);             
  60             } else {
  61                try {
  62                   parseHeaderLine(input);
  63                } catch (HTTPException e) {
  64                   // this isn't fatal, so keep on going
  65                   System.out.println(e);
  66                }
  67             }    
  68          }
  69          if (HTTPMethod == null) {
  70             System.out.println("Http request: No Request Line");
  71          }
  72       } catch (Exception e) {
  73          System.out.println("Failed HTTP request.  message=" + e);
  74 	   throw(e);		
  75       }
  76 
  77    }
  78    
  79    protected void parseRequestLine(String line) throws HTTPException {
  80       int pos;
  81       
  82       line = line.trim();
  83       pos = line.indexOf(' ');
  84       if (pos == -1) {
  85          HTTPException e = new HTTPException("Bad HTTP line.");
  86          throw(e);
  87       }
  88 
  89       HTTPMethod = line.substring(0, pos);
  90       
  91       line = line.substring(pos).trim();
  92       pos = line.indexOf(' ');
  93       
  94       if (pos == -1) {
  95        HTTPProtocol = "HTTP/0.9";
  96        HTTPPath = line;
  97       } else {
  98        HTTPPath = line.substring(0, pos);
  99        HTTPProtocol = line.substring(pos).trim();
 100       }
 101    }
 102 
 103    protected void parseHeaderLine(String line) throws HTTPException {
 104       int pos;
 105 
 106       pos = line.indexOf(':');
 107       if (pos == -1) {
 108          HTTPException e = new HTTPException("Bad HTTP line: " + line);
 109          throw(e);
 110       }
 111 
 112       String name = line.substring(0, pos);
 113       String value = line.substring(pos + 2);
 114 
 115       name = name.toLowerCase();
 116       HTTPHeaders.put(name, value);
 117    }
 118 
 119    protected String getHeader(String name) {
 120       String lower = new String(name).toLowerCase();
 121       lower.toLowerCase();
 122       return (String) HTTPHeaders.get(lower);
 123    }
 124       
 125    protected void readContent() throws IOException {
 126       String length = getHeader("Content-Length");
 127       int bytes;
 128       
 129       if (length == null) {
 130          return;
 131       }
 132 
 133 	int		size;
 134 	byte[]		b = new byte[2048];
 135 	StringBuffer	buffer = new StringBuffer();
 136 	while (true) {
 137 		size     = is.read(b, 0, 2048); 
 138 		if (size == -1) break;
 139 		buffer.append(new String(b, 0, size)); 
 140 		if (is.available() == 0) break; 
 141 	}
 142       HTTPContent = buffer.toString();
 143    }
 144 }